//Nathan Popham
//4003-573-01: Procedural Shading
//Lab 3: Task 3 
//
//sphericalTexture
// This shader wraps a texture around a sphere using its normal as the 
// x, y, and z coordinates. 
//
// An offset of PI/2 is applied to v specifically for the earth shader,
// to bring some continents into view.

surface
sphericalTexture ( string texturename = "";)
{
    color Ct = Cs;
    
    if(texturename != "")
    {
        normal Nf = normalize( faceforward(N, I) );
        
        float phi = atan(Nf[0], Nf[2]);
        float theta = acos(Nf[1]);

        float uu = phi / (2.0 * PI);
        float vv = 1 - theta / (2.0 * PI) + PI / 2; 

        color Crefl = color texture( texturename, uu, vv);
        
        Ct = Crefl;
    }


    Oi = Os;
    Ci = Ct * Oi;

}
